<?php
namespace app\api\service;


use app\common\model\tang\Config as CompanyConfig;
use think\cache\driver\Redis;
use think\Exception;

class JinDie{
    public function __construct(){

        $this->getToken = "http://60.174.231.230:14571/api/login";  //获取登录token
        $this->importVoucher = "http://60.174.231.230:14571/api/importVoucher";//凭证导入
        $this->username = "A0239";//账号
        $this->password = "ahzt1111";//密码
        $this->Electricity = "http://60.174.231.230:14571/api/Electricity";//获取电量
        $this->get_Points = "http://60.174.231.230:14571/api/get_Points";//获取电量
    }
    /**
     * 获取电量
     */
    public function get_Points($data){
        $result =Request::httpRequest($this->get_Points,'POST',json_encode($data),[]);
        return json_decode($result,true);
    }
    /**
     * 获取电量
     */
    public function getElectricity($data){
        $result =Request::httpRequest($this->Electricity,'POST',json_encode($data),[]);
        return json_decode($result,true);
    }
    /**
     * 获取token
     */
    public function getToken(){
//        $data['username'] = $this->username;
//        $data['password'] = $this->password;
        $result = (new CompanyConfig())->find(1);
        if(empty($result['jindie_account'])||empty($result['jindie_password'])){
            throw new Exception('请配置金蝶账号和密码');
        }
        $data['username'] = $result['jindie_account'];
        $data['password'] = $result['jindie_password'];
        $redis = (new Redis(config('queue.connections.redis')));
        $token = $redis->get('JINDIE');
        if($token){
            return $token;
        }else{
            $result =Request::httpRequest($this->getToken,'POST',json_encode($data),[]);
            $result = json_decode($result,true);
            if($result['code']==0){
                $redis->set('JINDIE',$result['data'],7100);
                return $result['data'];
            }else{
                return $result['message'];
            }
        }

    }
    /**
     * 传入凭证
     */
    public function importVoucher($param,$isverify=0,$isImpCashflow=0){
        $data['SessionId'] =$this->getToken();
        $data['WSWSVoucher'] =$param;
        $data['isverify'] = $isverify;
        $data['isImpCashflow'] = $isImpCashflow;
        $result =Request::httpRequest($this->importVoucher,'POST',json_encode($data),[]);
        return json_decode($result,true);
    }
}